import time

from selenium.webdriver.common.by import By

from common.utils import chromeDriver


class Login:
    driver = ""
    url = ""
    def __init__(self):
        self.url = "http://8.155.1.153/?s=user/logininfo.html"
        self.driver = chromeDriver.driver
        self.driver.get(self.url)

    def LoginTest(self, username, password, flag):
        #清空输入框
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-login-container.am-padding-top-0.am-padding-bottom-0.theme-data-edit-event > div > div.am-container > div.am-u-sm-12.am-u-md-6.am-u-lg-4.user-right-container > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(1) > input"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-login-container.am-padding-top-0.am-padding-bottom-0.theme-data-edit-event > div > div.am-container > div.am-u-sm-12.am-u-md-6.am-u-lg-4.user-right-container > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(2) > input"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-login-container.am-padding-top-0.am-padding-bottom-0.theme-data-edit-event > div > div.am-container > div.am-u-sm-12.am-u-md-6.am-u-lg-4.user-right-container > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(3) > input"))

        #输入用户名，密码，验证码
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-login-container.am-padding-top-0.am-padding-bottom-0.theme-data-edit-event > div > div.am-container > div.am-u-sm-12.am-u-md-6.am-u-lg-4.user-right-container > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(1) > input").send_keys(username)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-login-container.am-padding-top-0.am-padding-bottom-0.theme-data-edit-event > div > div.am-container > div.am-u-sm-12.am-u-md-6.am-u-lg-4.user-right-container > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(2) > input").sned_keys(password)
        img_text = chromeDriver.getverifycode(self.driver)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-login-container.am-padding-top-0.am-padding-bottom-0.theme-data-edit-event > div > div.am-container > div.am-u-sm-12.am-u-md-6.am-u-lg-4.user-right-container > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(3) > input").sned_keys(img_text)

        if flag:
            img_text = chromeDriver.getverifycode(self.driver)
            self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-login-container.am-padding-top-0.am-padding-bottom-0.theme-data-edit-event > div > div.am-container > div.am-u-sm-12.am-u-md-6.am-u-lg-4.user-right-container > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-form-group-refreshing.am-margin-top-main.am-form-input-material.am-form-error > input").send_keys(img_text)

        #点击登录
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-login-container.am-padding-top-0.am-padding-bottom-0.theme-data-edit-event > div > div.am-container > div.am-u-sm-12.am-u-md-6.am-u-lg-4.user-right-container > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-form-group-refreshing.am-margin-top-lg.am-padding-0 > button").click()

        #添加屏幕截图
        chromeDriver.GetScreenShot()


    #登录成功测试
    def LoginSuccess(self):
        self.LoginTest("209779", "123456", True)

        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-search.white.am-hide-sm-only > div > div.logo-big.theme-data-edit-event > a > img")

    #登录失败测试
    def LoginFailed(self):
        #用户名长度<2字符登录
        self.LoginTest("2", "123456", True)
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > div > a > img")
        #用户名长度>18字符登录
        self.LoginTest("209779209779209779209779209779", "123456", True)
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > div > a > img")
        #不写用户名登入
        self.LoginTest("", "123456", True)
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > div > a > img")
        #错误的用户名登录
        self.LoginTest("20977", "123456", True)
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > div > a > img")
        #密码长度小于6字符登录
        self.LoginTest("209779", "12345", True)
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > div > a > img")
        #密码长度大于18字符登录
        self.LoginTest("209779", "123456123456123456123456123456", True)
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > div > a > img")
        #不写密码登入
        self.LoginTest("209779", "", True)
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > div > a > img")
        #错误的密码登录
        self.LoginTest("209779", "123456", True)
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > div > a > img")
        #不写验证码登入
        self.LoginTest("209779", "123456", False)
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > div > a > img")
